package com.smackkitty.haikustudio;

import com.smackkitty.haikustudio.data.Haiku;
import com.smackkitty.haikustudio.data.validation.SyllableValidator;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.ClipboardManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Write_Haiku extends Activity implements Runnable {
	private ClipboardManager clipboard;
	private Haiku haiku;
	private TextView title;
	private TextView line1_err;
	private TextView line2_err;
	private TextView line3_err;
	private EditText line1;
	private EditText line2;
	private EditText line3;
	private int line1_cnt = -5;
	private int line2_cnt = -7;
	private int line3_cnt = -5;
	private ProgressDialog pd;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.write_haiku);
		
		clipboard =  (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
		
		title = ((TextView) findViewById(R.id.haiku_title));
		line1_err = ((TextView) findViewById(R.id.haiku_line1_error));
		line2_err = ((TextView) findViewById(R.id.haiku_line2_error));
		line3_err = ((TextView) findViewById(R.id.haiku_line3_error));
		line1 = ((EditText) findViewById(R.id.haiku_line1));
    	line2 = ((EditText) findViewById(R.id.haiku_line2));
    	line3 = ((EditText) findViewById(R.id.haiku_line3));
    	
    	line1_err.setText(String.valueOf(line1_cnt));
    	line2_err.setText(String.valueOf(line2_cnt));
    	line3_err.setText(String.valueOf(line3_cnt));
    	
    	this.haiku = new Haiku(this);
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    	super.onCreateOptionsMenu(menu);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.write_menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    		case R.id.menu_clear:
    			this.menu_clear_click();
    			Toast.makeText(this, "Haiku cleared", Toast.LENGTH_SHORT).show();
    			return true;
    		case R.id.menu_copy:
    			this.menu_copy_click();
    			Toast.makeText(this, "Haiku copied to clipboard", Toast.LENGTH_SHORT).show();
    			return true;
    		case R.id.menu_save:
    			this.menu_save_click();
    			return true;
    		case R.id.menu_verify:
    			this.menu_verify_click();
    			return true;
    	}
    	return false;
    }
    
    private void menu_clear_click(){
    	this.line1.setText("");
    	this.line2.setText("");
    	this.line3.setText("");
    }
    
    private void menu_copy_click(){
		clipboard.setText(line1.getText().toString() + " / " +  line2.getText().toString() + " / " + line3.getText().toString());
    }
    
    private void menu_save_click(){
    	this.haiku.setLine1(this.line1.toString());
    	this.haiku.setLine2(this.line2.toString());
    	this.haiku.setLine3(this.line3.toString());
    	
    	AlertDialog.Builder alert = new AlertDialog.Builder(this);
    	alert.setTitle("Save");
    	alert.setMessage("Please enter a title");

    	// Set an EditText view to get user input 
    	final EditText input = new EditText(this);
    	input.setText(this.haiku.getTitle());
    	alert.setView(input);

    	if(this.haiku.getTitle() == ""){
        	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        	public void onClick(DialogInterface dialog, int whichButton) {
        	    String value = input.getText().toString();
        	    
        	    haiku.setTitle(value);
        	    haiku.Save();
        	  }
        	});

        	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        	  public void onClick(DialogInterface dialog, int whichButton) {
        	    // Canceled.
        	  }
        	});    		
    	}else{
        	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            	public void onClick(DialogInterface dialog, int whichButton) {
            	    String value = input.getText().toString();
            	    
            	    haiku.setTitle(value);
            	    haiku.Update();
            	  }
            	});

            	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            	  public void onClick(DialogInterface dialog, int whichButton) {
            	    // Canceled.
            	  }
            	});
    	}
      	alert.show();
    }   
    
    private void menu_verify_click(){
    	pd = ProgressDialog.show(this, "Verifying Haiku", "Counting syllables...", true, false);
    	Thread thread = new Thread(this);
    	thread.start();
    }
    
    public void run(){
    	SyllableValidator val = new SyllableValidator();
    	
    	line1_cnt = val.SyllableAccuracy(line1.getText().toString(), 5);
    	line2_cnt = val.SyllableAccuracy(line2.getText().toString(), 7);
    	line3_cnt = val.SyllableAccuracy(line3.getText().toString(), 5);
    	
    	handler.sendEmptyMessage(0);
    }
    
    private Handler handler = new Handler(){
    	@Override
    	public void handleMessage(Message msg){
    		pd.dismiss();

        	line1_err.setText(String.valueOf(line1_cnt));
        	line2_err.setText(String.valueOf(line2_cnt));
        	line3_err.setText(String.valueOf(line3_cnt));
        	
    	}
    };
}
